<link href="https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.js"></script>
<table id="table"></table>
<script>
    var $table = $('#table');
    $(function () {
        $table.bootstrapTable({
            classes: "table table-bordered table-hover table-striped",
            method: 'post',
            striped: true,
            sortable: false,
            url: "/admin/overviews/data?company=<%=@company%>",
            sidePagination: "server",
            showFooter: true,
            columns: [
                [
                    {
                        title: '日期',
                        field: 'report_date',
                        align: 'center',
                        valign: "middle",
                        rowspan: 2
                    },
                    {
                        title: '集团',
                        field: 'company',
                        align: 'center',
                        valign: "middle",
                        rowspan: 2
                    },
                    {
                        title: '合作门店',
                        align: 'center',
                        colspan: 3
                    },
                    {
                        title: '课数/名额',
                        align: 'center',
                        colspan: 3
                    },
                    {
                        title: '上座率',
                        align: 'center',
                        colspan: 3
                    },
                    {
                        title: '应收金额',
                        field: 'credit',
                        align: 'center',
                        valign: "middle",
                        rowspan: 2
                    },
                    {
                        title: '流水金额',
                        field: 'pay_credit',
                        align: 'center',
                        valign: "middle",
                        rowspan: 2
                    },
                    {
                        title: '实收金额',
                        field: 'actually_credit',
                        align: 'center',
                        valign: "middle",
                        rowspan: 2
                    },
                    {
                        title: '人均消费',
                        field: 'pay_member',
                        align: 'center',
                        valign: "middle",
                        rowspan: 2,
                        formatter: function (data, row) {
                            if (data > 0) {
                                return (row.actually_credit / data).toFixed(2)
                            } else {
                                return "0.00"
                            }
                        }
                    }
                ],
                [
                    {
                        title: '全部',
                        field: 'all_stores',
                        align: 'center',
                        footerFormatter: sumFormatter
                    },
                    {
                        title: '免费',
                        field: 'free_stores',
                        align: 'center'
                    },
                    {
                        title: '收费',
                        field: 'tuition_stores',
                        align: 'center'
                    },
                    {
                        title: '全部',
                        field: 'all_course',
                        align: 'center',
                        formatter: function (data, row) {
                            return data + "/" + row.all_place
                        }
                    },
                    {
                        title: '免费',
                        field: 'free_course',
                        align: 'center',
                        formatter: function (data, row) {
                            return data + "/" + row.free_place
                        }
                    },
                    {
                        title: '收费',
                        field: 'tuition_course',
                        align: 'center',
                        formatter: function (data, row) {
                            return data + "/" + row.tuition_place
                        }
                    },
                    {
                        title: '全部',
                        field: 'all_appointed',
                        align: 'center',
                        formatter: function (data, row) {
                            if (data > 0) {
                                return ((data / row.all_place) * 100).toFixed(2) + "%"
                            } else {
                                return '0.00%'
                            }
                        }

                    },
                    {
                        title: '免费',
                        field: 'free_appointed',
                        align: 'center',
                        formatter: function (data, row) {
                            if (data > 0) {
                                return ((data / row.free_place) * 100).toFixed(2) + "%"
                            } else {
                                return '0.00%'
                            }
                        }
                    },
                    {
                        title: '收费',
                        field: 'tuition_appointed',
                        align: 'center',
                        formatter: function (data, row) {
                            if (data > 0) {
                                return ((data / row.tuition_place) * 100).toFixed(2) + "%"
                            } else {
                                return '0.00%'
                            }
                        }
                    }
                ]
            ],
            onLoadSuccess: function (data) {
                var rows = data.rows;
                var report_date = "";
                var startIndex = 0;
                for (var i = 0; i < rows.length; i++) {
                    var row = rows[i];
                    if (report_date === "") {
                        report_date = row["report_date"];

                    } else {
                        if (report_date !== row["report_date"]) {
                            $table.bootstrapTable('mergeCells', {
                                index: startIndex,
                                field: 'report_date',
                                rowspan: i - startIndex
                            });
                            startIndex = i;
                            report_date = row["report_date"];
                        }
                    }

                }
                $table.bootstrapTable('mergeCells', {
                    index: startIndex,
                    field: 'report_date',
                    rowspan: rows.length - startIndex
                });
            },
        })
    });
    function sumFormatter(data) {
        var field = this.field;
        return data.reduce(function (sum, row) {
            console.log(sum);
            return (sum) + (parseInt(row[field]) || 0);
        }, 0);
    }
</script>